if(!document.createDOM)
	document.createDOM = function(tagName,options={}){
	if(options.id){
		let hasCreated = this.query(options.id)
		if(hasCreated)return hasCreated;
	}
	let dom=this.createElement(tagName);
	for(let k in options)dom.setAttribute(k,options[k]);
	return dom;
}

if(!document.appendStyle)
	document.appendStyle = function(cssText,name){
		let style = this.createElement("style")
		style.innerHTML = cssText
		if(name)
		style.setAttribute("pk",name)
		this.query("head").appendChild(style)
		return style
}

if(!document.pageRect)
	document.pageRect = function(){
		let pr = 1//window.devicePixelRatio
		return { 
			width : this.documentElement.clientWidth*pr,
			height: this.documentElement.clientHeight*pr
		}
	}
if(!String.prototype.template)
	Reflect.defineProperty(String.prototype,'template',{
		value:function(data){
			if(data instanceof Array)return data.map((d,i)=>{
				return this.replace(/{(.*?)}/g,(a,b)=>{
					let isdw = /^(\w|\d)+$/.test(b)
					let arg = b.match(/^(\w|\d)+/)[0]
					let r = isdw?d[b]:new Function(arg,`return ${b}`)(d[arg])
					return r||""
				})
			}).join("")
			else return this.replace(/{(.*?)}/g,(a,b)=>{
					let isdw = /^(\w|\d)+$/.test(b)
					let arg = b.match(/^(\w|\d)+/)[0]
					let r = isdw?data[b]:new Function(arg,`return ${b}`)(data[arg])
					return r||""
				})
		}
	})



if(!Object.prototype.merge)
	Reflect.defineProperty(Object.prototype,"merge",{
		value:function(...r){
			for(let k=0;k<r.length;k++){
				let ri=r[k]
				for(let j in ri)
					this[j] = Object.isObject(ri[j])?Object.assign(this[j]||{},ri[j]):ri[j]
			}
		}
	})
if(!Object.prototype.assignPrefix)
	Reflect.defineProperty(Object.prototype,"assignPrefix",{
		value : function(...b){
			let target = this.assign()
			let prefix = b[b.length-1]
			if(b.length==1){
				let obj = {}
				for(let k in this)
					obj[prefix+k] = this[k]
				return obj
			}
			
			for(let i=0;i<b.length-1;i++){
				let obj = b[i]
				for(let k in obj){
					if(obj[k]!==undefined)
						target[prefix+k] = obj[k]
				}
			}
			return target
		}
	})
if(!Object.prototype.assign)
	Reflect.defineProperty(Object.prototype,"assign",{
		value : function(...b){
			return Object.assign(JSON.parse(JSON.stringify(this)),...b)
		}
	})
if(!Object.isObject)
	Reflect.defineProperty(Object,"isObject",{
		value : function(v){
			try{
				return v.constructor.name==="Object"
			}catch(e){
				return false
			}
		}
	})

if(!MouseEvent.prototype.epos){
	Reflect.defineProperty(MouseEvent.prototype,"epos",{
		value:function(e){
			let {pageX,pageY} = this
			let {left,top} = this.target.getBoundingClientRect()
			let {scrollLeft,scrollTop} = document.documentElement
			return {
				x:pageX-left-scrollLeft,
				y:pageY-top-scrollTop
			}
		}
	})
}

if(!Node.prototype.query)
	Reflect.defineProperty(Node.prototype,"query",{
		value:function(s){
			try{
				return this.querySelector(s)
			}catch(e){
				return null
			}
		}
	})

if(!Function.prototype.functional)
Reflect.defineProperty(Function.prototype,'functional',{
	value:function(ctx){
			clearTimeout(this.__timer)
			this.__timer=setTimeout(()=>{
				Reflect.apply(this,ctx,[...arguments].slice(1))
			},300)
	}
})
if(!Array.prototype.last){
	Reflect.defineProperty(Array.prototype,"last",{
		value:function(e){
			return this[this.length-1]
		}
	})
}
if(!Array.prototype.first){
	Reflect.defineProperty(Array.prototype,"first",{
		value:function(e){
			return this[0]
		}
	})
}
if(!Array.prototype.isEmpty){
	Reflect.defineProperty(Array.prototype,"isEmpty",{
		value:function(e){
			return this.length===0
		}
	})
}

if(!Array.prototype.clone){
	Reflect.defineProperty(Array.prototype,"clone",{
		value:function(e){
			return JSON.parse(JSON.stringify(this))
		}
	})
}
